package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.EmployeeService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * @Author dml
 * Date on 2022/6/7 0007
 */


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DishService dishService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {

        // 明文密码处理

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        // 数据库查询
        LambdaQueryWrapper<Employee> elqw = new LambdaQueryWrapper<>();
        elqw.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(elqw);

        // 查询账户
        if (one == null) {
            return R.error("未查询到该账户");
        }
        // 密码对比 one.getPassword() sql中的密码  password是页面转换后的密码
        if (!one.getPassword().equals(password)) {
            return R.error("密码输入错误");
        }
        // 查账户信息
        if (one.getStatus() == 0) {
            return R.error("该账户已禁用");
        }
        // 将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功!");
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpServletRequest request) {
        log.info("新增员工,员工信息:{}", employee.toString());

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        // 获取 注册id
//        Long empId = (Long)request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);

        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page={},pageSize = {},name = {}", page, pageSize, name);
        // 分页构造器
        Page pageInfo = new Page(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper();
        // 过滤条件 name不为空的时候 开启查询
        qw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        // 排序
        qw.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo, qw);

        // 执行查询 传递
        return R.success(pageInfo);
    }

    // 修改员工信息
    @PutMapping
    public R<String> add(@RequestBody Employee employee, HttpServletRequest request) {
        log.info(employee.toString());
        long id = Thread.currentThread().getId();
        log.info("线程Id为:", id);
        // 获取操作人id
//        Long empId = (Long)request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("修改成功！");
    }


    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        log.info("根据id查询");
        Employee byId = employeeService.getById(id);
        if (byId != null) {
            return R.success(byId);
        } else {
            return R.error("未查询到");
        }
    }


}